/**
 ******************************* ICE *******************************
 * ========================================================================
 * Project:		iceservices
 * Package:		org.ice4apps.service
 * File:		SystemService.java
 * Author: 		robin
 * Date: 		Feb 14, 2009
 * Description:	
 * ========================================================================
 * 
 */

package org.ice4apps.service;

import java.util.List;
import java.util.UUID;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import org.ice4apps.service.bean.EnterpriseAddServiceRequest;
import org.ice4apps.service.bean.EnterpriseGetServiceResponse;
import org.ice4apps.service.bean.EnterpriseListGetServiceResponse;
import org.ice4apps.service.bean.EntperpriseListEntry;
import org.ice4apps.service.bean.LoginServiceRequest;
import org.ice4apps.master.dao.Client;
import org.ice4apps.master.dao.Enterprise;
import org.ice4apps.master.dao.Session;
import org.ice4apps.master.dao.SystemAdmin;
import org.ice4apps.master.dataservices.IceMasterDataServiceLocator;
import org.ice4apps.talk.dao.TalkEnterprise;
import org.ice4apps.talk.dataservices.IceTalkDataServiceLocator;
import org.ice4apps.util.IceLog;

@Stateless
public class IceSystemService extends IceEnterpriseService implements IceSystemServiceLocal {

    /* (non-Javadoc)
     * @see org.ice4apps.provisioning.service.SystemServiceLocal#login(org.ice4apps.provisioning.service.bean.LoginRequest)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String login (LoginServiceRequest req) {
        req.validate ();
        try {
            Client client = IceMasterDataServiceLocator.getIceMasterDataService ().getClientByKey (req.getClient ());
            SystemAdmin admin = IceMasterDataServiceLocator.getIceMasterDataService ().getSystemAdminByUsername (req.getUserName ());
            Session session = new Session ();
            session.setClient (client);
            session.setId (UUID.randomUUID ().toString ());
            session.setLoginId (admin.getId ());
            session.setLoginRefreshTime (System.currentTimeMillis ());
            session.setType (0);
            IceMasterDataServiceLocator.getIceMasterDataService ().addSession (session);
            return session.getId ();
        } catch (Exception ex) {
            IceLog.error (IceSystemService.class, ex, "Exception");
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceSystemServiceLocal#logout(java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void logout (String sessionId) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().removeSession (sessionId);
        } catch (Exception ex) {
        }
        
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceSystemServiceLocal#addEnterprise(org.ice4apps.service.bean.Enterprise)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addEnterprise (String sessionId, EnterpriseAddServiceRequest enterprise) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
        } catch (Exception ex1) {
        }
        enterprise.validate ();
        try {
            Enterprise ent = new Enterprise ();
            String id = UUID.randomUUID ().toString ();
            ent.setId (id);
            ent.setName (enterprise.getName ());
            Long creationDate = System.currentTimeMillis ();
            ent.setCreationdate (creationDate);
            ent.setFeatures (Long.MAX_VALUE);
            IceMasterDataServiceLocator.getIceMasterDataService ().addEnterprise (ent);
            TalkEnterprise talkEnt = new TalkEnterprise ();
            talkEnt.setId (id);
            talkEnt.setCreationdate (creationDate);
            talkEnt.setFeatures (Long.MAX_VALUE);
            IceTalkDataServiceLocator.getIceTalkDataService ().addEnterprise (talkEnt);
        } catch (Exception ex) {
        }
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceSystemServiceLocal#deleteEnterprise(java.lang.String, java.lang.String)
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void deleteEnterprise (String sessionId, String name) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
        } catch (Exception ex1) {
        }
        try {            
            String id = IceMasterDataServiceLocator.getIceMasterDataService ().getEnterpriseByName (name).getId ();
            IceMasterDataServiceLocator.getIceMasterDataService ().deleteEnterprise (id);
            IceTalkDataServiceLocator.getIceTalkDataService ().deleteEnterprise (id);
        } catch (Exception ex) {
        }
        
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceSystemServiceLocal#getEnterprise(java.lang.String, java.lang.String)
     */
    public EnterpriseGetServiceResponse getEnterprise (String sessionId,
                                                       String name) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
        } catch (Exception ex1) {
        }
        try {            
            Enterprise ent = IceMasterDataServiceLocator.getIceMasterDataService ().getEnterpriseByName (name);
            EnterpriseGetServiceResponse resp = new EnterpriseGetServiceResponse();
            resp.setName (ent.getName ());
            return resp;
        } catch (Exception ex) {
        }
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceSystemServiceLocal#getEnterpriseList(java.lang.String)
     */
    public EnterpriseListGetServiceResponse getEnterpriseList (String sessionId) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
        } catch (Exception ex1) {
        }
        try {      
            EnterpriseListGetServiceResponse resp = new EnterpriseListGetServiceResponse ();
            List<Enterprise> list = IceMasterDataServiceLocator.getIceMasterDataService ().getEnterpriseList ();
            for (Enterprise ent : list) {
                EntperpriseListEntry entry = new EntperpriseListEntry ();
                entry.setName (ent.getName ());
                IceLog.info (IceSystemService.class, "name: "+ent.getName ());
                resp.add (entry);                
            }
            return resp;
            
        } catch (Exception ex) {
        }
        return null;
    }
}
